package com.wxy.zhuzhoumenu;


import com.ak.zhuzhou.R;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.wxy.zhuzhou.CircleImageView;
import com.wxy.zhuzhou.MainActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuCommentAdapter extends BaseAdapter{
	private JSONArray data;
	private Context context;
	private SharedPreferences sp;
	private JSONObject dataItem;

	public MenuCommentAdapter(Context context , JSONArray data) {
		this.context = context;
		this.data = data;
		sp = PreferenceManager.getDefaultSharedPreferences(context);
	}

	class ViewHolder {
		CircleImageView avatar;
		TextView commentContent;
		TextView name;
		TextView commentTitle;
		TextView commentTime;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.getJSONObject(position).getString("newsId");
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.x_menu_comment_item, null);
			viewHolder = new ViewHolder();
			viewHolder.avatar = (CircleImageView) convertView
					.findViewById(R.id.user_comment_avatar);
			viewHolder.commentContent = (TextView) convertView
					.findViewById(R.id.user_comment_content);
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.user_comment_name);
			viewHolder.commentTitle = (TextView) convertView
					.findViewById(R.id.user_comment_new_title);
			viewHolder.commentTime = (TextView) convertView
					.findViewById(R.id.user_comment_time);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		dataItem = data.getJSONObject(position);
		 viewHolder.commentTitle.setText(dataItem.getString("newsTitle"));
		 viewHolder.commentContent.setText(dataItem.getString("content"));
		 viewHolder.commentTitle.setText(dataItem.getString("newsTitle"));
		 viewHolder.commentTime.setText(dataItem.getString("dateline"));
		 
		 if (sp != null && sp.getString("avatar", "") != null) {
				String avatarUrl = sp.getString("avatar", "");
				BitmapUtils bitmapUtils = new BitmapUtils(context);
				bitmapUtils.display(viewHolder.avatar , "http://tr.zzapi.gson.cn/" + avatarUrl);
			}
		 viewHolder.name.setText(sp.getString("userName", ""));
		 
		return convertView;
	}
	


}
